﻿using Fast;
using Fast.Extensions;
using Fast.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.StackExchangeRedis;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class StackExchangeRedisCacheServiceCollectionExtensions
    {
        public static IServiceCollection AddFastStackExchangeRedisCache(this IServiceCollection services, Action<RedisCacheOptions> setupAction = null)
        {
            services.AddFastDistributedCache();
            services.AddStackExchangeRedisCache(options =>
            {
                var redisConfiguration = App.Configuration["Redis:Configuration"];
                if (!redisConfiguration.IsNullOrEmpty())
                {
                    options.Configuration = redisConfiguration;
                }
                setupAction?.Invoke(options);
            });

            services.Replace(ServiceDescriptor.Singleton<IDistributedCache, FastRedisCache>());
            return services;
        }
    }
}
